/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 07 - 07
 * Time: 9:56
 */
class Solution1 {
    public int searchInsert(int[] nums, int target) {
        for(int i = 0; i < nums.length; i++) {
            if(target == nums[i]) {
                return i;
            }
            if((i == 0) && (target < nums[i])) {
                return 0;
            }
            if((i == nums.length - 1) && (target > nums[i])) {
                return nums.length;
            }
            if(target > nums[i] && target < nums[i + 1]) {
                return i + 1;
            }
        }
        return 0;//这一行是没用的，因为一定会在上面返回，这里加返回是为了防止报
    }             //错
}

class Solution2 {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while(left <= right) {
            int mid = left + (right - left) / 2;
            if(target < nums[mid]) {
                right = mid - 1;
            }else if(target > nums[mid]) {
                left = mid + 1;
            }else {
                return mid;
            }
        }
        return left;//因为插入的位置不能比0下标更前，所以返回left；若插入的下标
    }                //不能比nums.length-1更后，则返回right
}





